<template>
  <span>
  <b v-if="!outTime">
    剩余时间：{{m}}:{{s}}
  </b>
  <b v-else>
    已过期
  </b>
 </span>
</template>

<script>
import {leftTime} from '../common/js/util'
import {commonAjax, PLATFORM_PREFIX} from '../api/api'
export default {
  props: ['displayTime'],
  data () {
    return {
      m: 0,
      s: 0,
      outTime: false
    }
  },
  mounted () {
    const ORDER_OUT_DATE_TIME = 0
    let _this = this
    if (!this.displayTime) return
    commonAjax([], `${PLATFORM_PREFIX}.appService`, 'getServerTime').then(res => {
      if (res.data.code === 200) {
        let orderCreateTime = new Date(this.displayTime.replace(/-/g, '/')).getTime()
        let dist = new Date(res.data.body.serverTime.replace(/-/g, '/')) - new Date()
        let timer = setInterval(() => {
          let left = leftTime(ORDER_OUT_DATE_TIME, (dist + (new Date() - orderCreateTime)))
          if (left.m <= 0 && left.s <= 0) {
            _this.outTime = true
            _this.$emit('outdate')
            clearInterval(timer)
          } else {
            _this.m = left.m
            _this.s = left.s
          }
        }, 1000)
      }
    })
  }
}
</script>

